/*
 * Copyright (c) 2011, Georgia Tech Research Corporation
 * All rights reserved.
 *
 * Author: Tobias Kunz <tobias@gatech.edu>
 * Date: 05/2012
 *
 * Humanoid Robotics Lab      Georgia Institute of Technology
 * Director: Mike Stilman     http://www.golems.org
 *
 * Algorithm details and publications:
 * http://www.golems.org/node/1570
 *
 * This file is provided under the following "BSD-style" License:
 *   Redistribution and use in source and binary forms, with or
 *   without modification, are permitted provided that the following
 *   conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 *   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 *   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *   POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include <Eigen/Core>
#include <list>
#include <map>
#include "plan/path.h"

namespace ZORAL
{

    class PlannerTimeOptimalGeorgia
    {
    public:
        // Generates a time-optimal trajectory
        PlannerTimeOptimalGeorgia(const Path &path, const Eigen::VectorXd &maxVelocity,
                                  const Eigen::VectorXd &maxAcceleration, double timeStep = 0.001);

        ~PlannerTimeOptimalGeorgia();

        // Call this method after constructing the object to make sure the trajectory generation succeeded without
        // errors. If this method returns false, all other methods have undefined behavior.
        bool isValid() const;

        // Returns the optimal duration of the trajectory
        double getDuration() const;

        // Return the position/configuration or velocity vector of the robot for a given point in time within the
        // trajectory.
        Eigen::VectorXd getPosition(double time) const;
        Eigen::VectorXd getVelocity(double time) const;

        Eigen::VectorXd getCartesianPosition(double time);
        Eigen::VectorXd getCartesianVelocity(double time);

        // Outputs the phase trajectory and the velocity limit curve in 2 files for debugging purposes.
        void outputPhasePlaneTrajectory() const;

        struct TrajectoryStep
        {
            TrajectoryStep() = default;
            TrajectoryStep(double pathPos, double pathVel) : pathPos(pathPos), pathVel(pathVel)
            {
            }
            double pathPos{};
            double pathVel{};
            double time{};
        };

        inline std::list<TrajectoryStep> getTrajectory()
        {
            return trajectory;
        }

        inline std::list<TrajectoryStep>::const_iterator getTrajectoryByIndex(size_t index)
        {
            auto it = trajectory.begin();
            std::advance(it, index);
            return it; // NOLINT
        }

        inline std::vector<double> getSwitchingTimes()
        {
            return switchingTimes;
        }

        inline std::vector<int> getSwitchingTimesIndices()
        {
            return switchingTimesIndices;
        }

        inline void setOrientation(const double &t, const Eigen::VectorXd &v)
        {
            orientationInterpolations[t] = v;
        }

        // inline Eigen::VectorXd getOrientation(const double &t)
        // {
        //     return orientationInterpolations[t];
        // }

        inline Eigen::VectorXd getOrientation(const double &t)
        {
            // 处理最后一个点的情况
            if (t >= orientationInterpolations.rbegin()->first)
            {
                return orientationInterpolations.rbegin()->second; // 返回最后一个点的值
            }

            // 查找最近的两个时间点 t1 和 t2
            auto it_lower = orientationInterpolations.lower_bound(t); // 第一个不小于t的时间
            if (it_lower == orientationInterpolations.begin())
            {
                return it_lower->second; // 如果t比所有时间都小，直接返回第一个
            }

            auto it_upper = it_lower;
            --it_lower; // 找到t之前的一个时间

            // 计算线性插值
            double t1 = it_lower->first;
            double t2 = it_upper->first;
            Eigen::VectorXd v1 = it_lower->second;
            Eigen::VectorXd v2 = it_upper->second;

            // 计算插值系数
            double alpha = (t - t1) / (t2 - t1); // alpha在[0, 1]之间

            // 返回线性插值的结果
            return v1 + alpha * (v2 - v1);
        }

    private:
        bool getNextSwitchingPoint(double pathPos, TrajectoryStep &nextSwitchingPoint, double &beforeAcceleration,
                                   double &afterAcceleration);
        bool getNextAccelerationSwitchingPoint(double pathPos, TrajectoryStep &nextSwitchingPoint,
                                               double &beforeAcceleration, double &afterAcceleration);
        bool getNextVelocitySwitchingPoint(double pathPos, TrajectoryStep &nextSwitchingPoint,
                                           double &beforeAcceleration, double &afterAcceleration);
        bool integrateForward(std::list<TrajectoryStep> &trajectory, double acceleration);
        void integrateBackward(std::list<TrajectoryStep> &startTrajectory, double pathPos, double pathVel,
                               double acceleration);
        double getMinMaxPathAcceleration(double pathPosition, double pathVelocity, bool max);
        double getMinMaxPhaseSlope(double pathPosition, double pathVelocity, bool max);
        double getAccelerationMaxPathVelocity(double pathPos) const;
        double getVelocityMaxPathVelocity(double pathPos) const;
        double getAccelerationMaxPathVelocityDeriv(double pathPos) const;
        double getVelocityMaxPathVelocityDeriv(double pathPos);

        std::list<TrajectoryStep>::const_iterator getTrajectorySegment(double time) const;

        Path path;
        Eigen::VectorXd maxVelocity;
        Eigen::VectorXd maxAcceleration;
        unsigned int n;
        bool valid;
        std::list<TrajectoryStep> trajectory;
        std::list<TrajectoryStep> endTrajectory; // non-empty only if the trajectory generation failed.

        static const double eps;
        const double timeStep;

        mutable double cachedTime;
        mutable std::list<TrajectoryStep>::const_iterator cachedTrajectorySegment;

        std::vector<double> switchingTimes;
        std::vector<int> switchingTimesIndices;
        std::map<double, Eigen::VectorXd> orientationInterpolations;
    };
} // namespace ZORAL
